package com.yesjpt.serv.impl;

import java.io.IOException;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson2.JSONObject;
import com.yesjpt.domain.FileInfo;
import com.yesjpt.serv.FileInfoServ;
import com.yesjpt.util.FileUtil;
import com.yesjpt.util.L;

@Service
public class FileInfoServImpl extends BaseServImpl implements FileInfoServ {
	
	@Override
	public FileInfo save(MultipartFile file) throws IOException {
		L.info(new Throwable().getStackTrace()[0]);
		//解析出文件名
		String fileOrigName = FileUtil.parseOriginName(file.getOriginalFilename());
		//md5
		String md5 = FileUtil.fileMd5(file.getInputStream());
		//相对路径：/yyyy/MM/dd/HHmmss/ + 文件名
		String url = FileUtil.getPath() + fileOrigName;
		String path = filesRootPath + url;//完整磁盘路径
		FileUtil.save(file, path);//执行保存
		long size = file.getSize();//大小
		String contentType = file.getContentType();//类型
		//实例化
		FileInfo vo = new FileInfo();
		vo.setId(md5);
		vo.setName(fileOrigName);
		vo.setContentType(contentType);
		vo.setSize(size);
		vo.setPath(path);
		vo.setUrl(url);
		vo.setFullUrl(filesRootUrl + url);
		vo.setType(contentType.startsWith("image/") ? 1 : 0);
		System.out.println("vo = " + vo + "，转换为JSON后为：\n" + JSONObject.toJSONString(vo));
		L.i("file.name = " + vo.getName() + ", 文件上传到：" + path + ", url = " + url + ", fullUrl = " + vo.getFullUrl());
		return vo;
	}
}

















